#!/bin/python3
#@名称：GNU/Linux ACPI性能模式切换器
#@作者：星橙猫
#@许可：CC BY-SA 4.0

def read_acpi():
    #读取acpi所有的性能选项
    choices={}
    modes=open('/sys/firmware/acpi/platform_profile_choices','r',encoding="utf-8")
    acpi_modes=modes.read().split(' ')
    k=0
    print('所有的性能模式：\n')
    for i in acpi_modes:
        k+=1
        #print('第'+str(k)+'个模式：',end='')
        #print(i)
        choices[k]=i.strip('\n')
    return choices
def write_acpi(u_c,dict1):
    #按照序号写入acpi性能选项
    x=open('/sys/firmware/acpi/platform_profile','w',encoding="utf-8")
    x.write(dict1[int(u_c)])
    x.flush()
    x.close()
while True:
    print('欢迎使用Linux_ACPI_Mode切换器v2版本.\n作者：橙子猫<bingfengfsx@qq.com>\n按照CC BY-SA 4.0许可')
    with open('/sys/firmware/acpi/platform_profile','r',encoding="utf-8") as d:
        print('请选择您需要的性能模式序号，按e则是退出：\n>当前性能模式：'+str(d.read()))
    acpi_choices=read_acpi()
    for i1,i2 in acpi_choices.items():
        print(str(i1)+'：'+str(i2))
    #print('测试：'+str(list(acpi_choices.keys())[0]))
    xuhao=list(map(str,list(acpi_choices.keys())))
    user_choice=str(input('>'))
    #print(user_choice,xuhao)
    if user_choice in xuhao:
        try:write_acpi(user_choice,acpi_choices)
        except PermissionError as e1:
            print("请使用root权限运行此脚本.")
            break
    elif user_choice=='e':break
    else:print('输入错误，请重新选择');continue
input('请按Enter键退出.')
exit(0)
